package com.hsqyz.common.utils.health;

import com.hsqyz.common.utils.spring.SpringUtils;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import javax.servlet.http.HttpServletRequest;

/**
 * 健康状态检查工具类
 *
 * @author lcz
 */
public class HealthUtil {

    /**
     * 应用服务名称
     */
    private static final String applicationName = SpringUtils.getApplicationName();

    public static ResponseEntity<String> getStringResponseEntity(HttpServletRequest request) {
        // 获取请求头中的 User-Agent
        String userAgent = request.getHeader("User-Agent");
        // 判断是否是浏览器请求
        if (isBrowser(userAgent)) {
            // 如果是浏览器请求，返回 HTML 内容
            String htmlContent = "<!DOCTYPE html>\n" +
                "<html lang=\"zh-CN\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                "    <title>欢迎来到 " + applicationName + "</title>\n" +
                "    <style>\n" +
                "        body {\n" +
                "            font-family: Arial, sans-serif;\n" +
                "            margin: 0;\n" +
                "            padding: 0;\n" +
                "            display: flex;\n" +
                "            justify-content: center;\n" +
                "            align-items: center;\n" +
                "            height: 100vh;\n" +
                "            background-color: #f0f2f5;\n" +
                "        }\n" +
                "        .container {\n" +
                "            text-align: center;\n" +
                "            background: white;\n" +
                "            padding: 20px;\n" +
                "            border-radius: 8px;\n" +
                "            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);\n" +
                "        }\n" +
                "        h1 {\n" +
                "            color: #333;\n" +
                "        }\n" +
                "        p {\n" +
                "            color: #666;\n" +
                "        }\n" +
                "        a {\n" +
                "            color: #007bff;\n" +
                "            text-decoration: none;\n" +
                "        }\n" +
                "        a:hover {\n" +
                "            text-decoration: underline;\n" +
                "        }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <div class=\"container\">\n" +
                "        <h1>" + applicationName + " 服务启动成功</h1>\n" +
                "        <p>欢迎访问健康状态检查页面</p>\n" +
                "    </div>\n" +
                "</body>\n" +
                "</html>";
            return ResponseEntity.ok()
                .contentType(MediaType.TEXT_HTML)
                .body(htmlContent);
        } else {
            // 如果是接口工具请求，返回 JSON 内容
            String jsonResponse = "{\"code\": 200, \"msg\": \" " + applicationName + "服务启动成功！\"}";

            return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_JSON)
                .body(jsonResponse);
        }
    }

    /**
     * 判断是否是浏览器请求
     *
     * @param userAgent 请求头中的 User-Agent
     * @return 结果
     */
    public static boolean isBrowser(String userAgent) {
        // 检查 User-Agent 是否包含常见浏览器标识
        return userAgent != null && (userAgent.contains("Mozilla") || userAgent.contains("Chrome") || userAgent.contains("Safari"));
    }

}
